import Vue from "vue";
import Vuex from "vuex";
import {
	userCartCount,
	cartQueryByUser
} from '@/api/shop.js'
import {
	$shopImgUrl
} from '@/service/request.js'
Vue.use(Vuex);
const vuexStore = new Vuex.Store({
	state: {
		userInfo: uni.getStorageSync('userInfo') || {},
		login: uni.getStorageSync('login') || 0,
		itemInfo: {},
		carNum: 0,
		shopIndex: 0,
		carList: [],
		payList:[],
		address:uni.getStorageSync('address') || {},
		carSqList:[],
		carSxList:[],
		carBzList:[]
	},
	mutations: {
		setAddress(state, obj) {
			state.address = obj
			uni.setStorageSync('address', obj)
		},
		setShopIndexm(state, val) {
			state.shopIndex = val
		},
		setItemInfo(state, obj) {
			state.itemInfo = obj
		},
		setUserInfo(state, obj) {
			state.userInfo = obj
			uni.setStorageSync('userInfo', obj)
		},
		setLogin(state, val) {
			state.login = val
			uni.setStorageSync('login', val)
		},
		setCarNum(state, val) {
			state.carNum = val
		},
		setCarList(state, list) {
			state.carList = list
		},
		setCarSqList(state, list) {
			state.carSqList = list
		},
		setCarSxList(state, list) {
			state.carSxList = list
		},
		setCarBzList(state, list) {
			state.carBzList = list
		},
		setPayList(state, list) {
			state.payList = list
		}
	},
	actions: {
		userCartCount({
			state,
			commit
		}) {
			userCartCount({
				idUser: state.userInfo.id,
				JSESSIONID: uni.getStorageSync('sessionid')
			}).then(res => {
				commit('setCarNum', res)
			})
		},
		cartQueryByUser({
			state,
			commit
		},buyInfo={}) {
			cartQueryByUser({
				idUser: state.userInfo.id,
				JSESSIONID: uni.getStorageSync('sessionid')
			}).then(res => {
				const car=[],sq=[],sx=[],bz=[]
				for (let i = 0; i < res.length; i++) {
					if(res[i].sku){
						if(res[i].sku.delFlag!=0||res[i].sku.isOnSale==0){
							sx.push(res[i])
							continue
						}
						if(res[i].sku.stock==0){
							sq.push(res[i])
							continue
						}
						if(res[i].sku.stock>0&&res[i].count>res[i].sku.stock){
							bz.push(res[i])
							continue
						}
					}
					if(res[i].goods){
						if(res[i].goods.delFlag!=0||res[i].goods.isOnSale==0){
							sx.push(res[i])
							continue
						}
						if(res[i].goods.stock==0){
							sq.push(res[i])
							continue
						}
						if(res[i].goods.stock>0&&res[i].count>res[i].goods.stock){
							bz.push(res[i])
							continue
						}
					}
					
					if(res[i].delFlag!=0||res[i].isOnSale==0){
						sx.push(res[i])
						continue
					}
					
					car.push(res[i])
				}
				commit('setCarList', car.map(item => {
					if(JSON.stringify(buyInfo)=='{}'){
						return {
							...item,
							src: $shopImgUrl + item.goods.pic,
							checked: true
						}
					}else{
						if(buyInfo.idSku){
							return {
								...item,
								src: $shopImgUrl + item.goods.pic,
								checked: buyInfo.idGoods==item.idGoods&&buyInfo.idSku==item.idSku
							}
						}else{
							return {
								...item,
								src: $shopImgUrl + item.goods.pic,
								checked: buyInfo.idGoods==item.idGoods
							}
						}
					}
				}))
				commit('setCarSxList',sx.map(item=>{
					return {
						...item,
						src: $shopImgUrl + item.goods.pic,
						checked: false
					}
				}))
				commit('setCarSqList',sq.map(item=>{
					return {
						...item,
						src: $shopImgUrl + item.goods.pic,
						checked: false
					}
				}))
				commit('setCarBzList',bz.map(item=>{
					return {
						...item,
						src: $shopImgUrl + item.goods.pic,
						checked: false
					}
				}))
				
				
				
			})
		},
	},
	getUser: {

	},
});

export default vuexStore
